package com.chookss.home.knowledge.adaper;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.home.document.DocumentBaseDirectorySecondActivity;
import com.chookss.home.entity.FolderEntity;
import com.chookss.home.entity.KnowledgeEntity;
import com.chookss.home.knowledge.BuildDirectoryActivity;
import com.chookss.home.knowledge.DirectoryDetailActivity;
import com.chookss.home.knowledge.KnowledgeBaseSecondActivity;
import com.chookss.tools.MyEvent;
import com.chookss.tools.MyToast;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.chookss.view.BottomListsDialog;
import com.chookss.view.SelectDialogListener;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.annotations.Nullable;

public class FolderAdapter extends BaseQuickAdapter<FolderEntity, BaseViewHolderEx> {
    private Activity context;
    private int level;//层级
    private boolean isTask = false;
    private ArrayList<String> nameList;//组织排列
    private String catalogCustomType;//自定义专属字段
    private String catalogType;//（0：全局 1: 题库 2:视频 3:资料库 4：图库 5：图文）
    private int type;//0知识库 2资料库
    private KnowledgeEntity levelOne;//一级目录数据
    private String resourceType = "";////        资源类型（1资料，2长视频，3短视频 6图片）
    private BottomListsDialog bottomListsFolderDialog = null;
    private List<String> dialogList = new ArrayList<>();
    private boolean permissions=false;//默认么有删除重命名权限
    public FolderAdapter(int layoutResId, Activity context, @Nullable List<FolderEntity> data, ArrayList<String> nameList, int level, String catalogCustomType,String catalogType,int type,KnowledgeEntity levelOne,String resourceType) {
        super(layoutResId, data);
        this.context = context;
        this.nameList = nameList;
        this.level = level;
        this.type = type;
        this.catalogCustomType = catalogCustomType;
        this.catalogType = catalogType;
        this.resourceType = resourceType;
        this.levelOne = levelOne;
    }

    @Override
    protected void convert(BaseViewHolderEx helper, FolderEntity entity) {
        RelativeLayout rlMore = helper.getView(R.id.rlMore);
        TextView tvItemTitle = helper.getView(R.id.tvItemTitle);
        TextView tvInit = helper.getView(R.id.tvInit);
        TextView tvInit2 = helper.getView(R.id.tvInit2);
        tvItemTitle.setText(entity.getName());
        if(!Utils.isNull(entity.getName())){
            if(entity.getName().equals("我的上传")){
                tvInit.setVisibility(View.VISIBLE);
                tvInit.setText("我共上传了"+entity.getFileNum() + "个文件");
                tvInit2.setVisibility(View.GONE);
            }else{
                if (Utils.isNull(entity.getFolderNum())) {
                    tvInit.setVisibility(View.GONE);
                } else {
                    tvInit.setVisibility(View.VISIBLE);
                    tvInit.setText(entity.getFolderNum() + "个文件夹");
                }
                if (Utils.isNull(entity.getFileNum())) {
                    tvInit2.setVisibility(View.GONE);
                } else {
                    tvInit2.setVisibility(View.VISIBLE);
                    tvInit2.setText(entity.getFileNum() + "个文件");
                }
            }
        }
        helper.getItemView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Urls.antiShake.check()) {
                    return;
                }
                toNext(entity);
            }
        });
        if(type==0&&level==1&&("-1".equals(catalogType))){
            rlMore.setVisibility(View.GONE);
        }else{
            rlMore.setVisibility(View.VISIBLE);
        }
        rlMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogList.clear();
                if(!permissions||"3".equals(entity.getParentCode())||"2".equals(entity.getParentCode())){
//                    个人文件和我的部门的第二级也不能删除编辑
                    dialogList.add("查看");
                    dialogList.add("详细信息");
                    dialogList.add("取消");
                }else{
                    dialogList.add("查看");
                    dialogList.add("重命名");
                    dialogList.add("详细信息");
                    dialogList.add("删除");
                    dialogList.add("取消");
                }
                bottomListsFolderDialog = new BottomListsDialog(context, dialogList, new SelectDialogListener() {
                    @Override
                    public void onCancel() {

                    }

                    @Override
                    public void onListItemClick(int position, List<String> list) {
                        if (list.get(position).equals("查看")) {
                            toNext(entity);
                        } else if (list.get(position).equals("重命名")) {
                          //重命名
                            Intent intent = new Intent(context, BuildDirectoryActivity.class);
                            intent.putExtra("type", 1);
                            intent.putExtra("parentCode", entity.getParentCode());
                            intent.putExtra("catalogCode", entity.getCode());
                            intent.putExtra("catalogType",catalogType);
                            intent.putExtra("catalogName", entity.getName());
                            intent.putStringArrayListExtra("nameList", nameList);
                            context.startActivity(intent);
                        } else if (list.get(position).equals("详细信息")) {
                          //详细信息
                            Intent intent = new Intent(context, DirectoryDetailActivity.class);
                            intent.putExtra("catalogCode", entity.getCode());
                            intent.putExtra("catalogType", catalogType);
                            context.startActivity(intent);
                        } else if (list.get(position).equals("删除")) {
                            //删除
                            delete(catalogType,entity.getCode());
                        }
                        bottomListsFolderDialog.dismiss();
                    }
                });
                bottomListsFolderDialog.show();
            }
        });
    }
    private void toNext(FolderEntity entity){
        if(type==0){
            Intent intent = new Intent(context, KnowledgeBaseSecondActivity.class);
            ArrayList<String> nameNewList = new ArrayList<>();//组织排列
            nameNewList.addAll(nameList);
            nameNewList.add(entity.getName());
            intent.putStringArrayListExtra("nameList", nameNewList);
            if (level==1) {
                //                        （0：全局 1: 题库 2:视频 3:资料库 4：图库 5：图文）
                if ("资料库".equals(entity.getName()) || "文件".equals(entity.getName())) {
                    intent.putExtra("catalogCustomType", "1");
                    intent.putExtra("catalogType", "3");
                } else if ("视频".equals(entity.getName())) {
                    intent.putExtra("catalogCustomType", "2");
                    intent.putExtra("catalogType", "2");
                } else if ("小视频".equals(entity.getName())) {
                    intent.putExtra("catalogCustomType", "3");
                    intent.putExtra("catalogType", "2");
                }else if ("图文".equals(entity.getName())) {
                    intent.putExtra("catalogCustomType", "5");
                    intent.putExtra("catalogType", "5");
                }else if ("图库".equals(entity.getName())) {
                    intent.putExtra("catalogCustomType", "6");
                    intent.putExtra("catalogType", "4");
                }else{
                    intent.putExtra("catalogCustomType", catalogCustomType);
                    intent.putExtra("catalogType", catalogType);
                }
            }else{
                intent.putExtra("catalogCustomType", catalogCustomType);
                intent.putExtra("catalogType", catalogType);
            }
            intent.putExtra("level", (level + 1));
            intent.putExtra("parentCode", entity.getCode());
            intent.putExtra("folderNum", entity.getFolderNum());
            intent.putExtra("levelOne", levelOne);
            intent.putExtra("resourceType", resourceType);
            context.startActivity(intent);
        }else{
            Intent intent = new Intent(context, DocumentBaseDirectorySecondActivity.class);
            ArrayList<String> nameNewList = new ArrayList<>();//组织排列
            nameNewList.addAll(nameList);
            nameNewList.add(entity.getName());
            intent.putStringArrayListExtra("nameList", nameNewList);
            intent.putExtra("level", (level + 1));
            intent.putExtra("parentCode", entity.getCode());
            intent.putExtra("catalogType", catalogType);
            intent.putExtra("folderNum", entity.getFolderNum());
            intent.putExtra("levelOne", levelOne);
            context.startActivity(intent);
        }
    }
    private void delete( String catalogType, String catalogCode) {
        Map<String, String> map = new HashMap<>();
        map.put("catalogCode", catalogCode);
        map.put("catalogType", catalogType);
        MyHttpRequest.getDefault().postRequestCompat(null, Urls.deleteCatalog, SignUtils2.makeSignStr(map), false, new ObserverCallback<String>() {
            @Override
            public void onSuccess(String data) throws Exception {
                JSONObject reObject = new JSONObject(data);
                if ("200".equals(reObject.getString("code"))) {
                    MyToast.show("删除成功");
                    EventBus.getDefault().post(new MyEvent("RefreshDirectory"));
                } else {
                    MyToast.show(reObject.getString("msg"));
                }
            }

            @Override
            public void onError(String errorMsg) {
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }

    public void setResourceType(String resourceType){
        this.resourceType=resourceType;
    }
    public void setPermissions(boolean permissions){
        this.permissions=permissions;
    }
}
